We then create TodoList that uses generics.ListAPIView. ListAPIView is a built-in generic class which creates a

read-only endpoint for model instances. ListAPIView requires two mandatory attributes which are serializer_class

and queryset.

When we specify TodoSerializer as the serializer class, we create a read-only endpoint for todo instances. There are

many other generic views available and we explore them later.

Analyze Code

def get_queryset(self):

user = self.request.user

return Todo.objects.filter(user=user).order_by('-created')

get_queryset returns the queryset of todo objects for the view. In our case, we specify the query set as all todos

which match the user. Additionally, we order the todos by the created date i.e. we show the latest todo first. You

can customize get_queryset to return the set of todos that you want.

Running our App

In Terminal, cd to the todobackend directory and run python3 manage.py runserver to test run your app.

Go to localhost:8000/api/todos and you can see the data in JSON format visualized (fig. 1)! Note you must be

logged in the admin section with your admin user, before accessing the previous route.

Figure 1

DRF provides this powerful visualization by default. There are lots of other functionality in this page that we will

explore throughout the book. As you add more todos via the Admin, they will show up in the API endpoint as you

reload the page.

And you just created your first API endpoint with Django Rest Framework! See how DRF generic views help us to

quickly implement these endpoints with little code?

But what about endpoints that allow us to create, update or delete? We will explore them in the following chapters.